<?php
namespace app\admin\controller;
class Store extends Base {
    public function __construct() {
        parent::__construct();
    }
    
    public function storeList() {
        if($this->noLogin()){
            return $this->noLoginReturn();
        }
        
        if($this->role != 1 && $this->role != 2) {
            return errorResult();
        }
        
        $params = request()->get();
        $page = isset($params['page']) ? intval($params['page']) : 1;
        $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : 20;
        $offset = ($page - 1) * $pageSize;
        
        $where = [];
        
        $storeName = isset($params['storeName']) ? trim($params['storeName']) : '';
        if($storeName !== '') {
            $where[] = ['store_name' , 'like', '%' . $storeName . '%'];
        }
        
        $storeAccount = isset($params['storeAccount']) ? trim($params['storeAccount']) : '';
        if($storeAccount !== '') {
            $where[] = ['store_account' , 'like', '%' . $storeAccount . '%'];
        }
        
        if($this->role == 2) {
            $where[] = ['agent_id' , '=', $this->userid];
        }
        
        $repData = [];
        $model = model('Store');
        
        $repData['list'] = $model
        ->where($where)
        ->field(['store_id', 'store_name', 'store_account', 'contact', 'mobile', 'service_telephone', 'profit_rate', 'total_amount', 'profit_amount', 'profit_online_amount', 'profit_coin_amount', 'profit_card_amount', 'create_time'])
        ->order('store_id desc')
        ->limit($offset, $pageSize)
        ->select();
        
        if(isset($params['init']) && $params['init'] == 1){
            $repData['total'] = $model->where($where)->count();
        }
        
        return successResult($repData);
        
    }
    
    public function save() {
        if($this->noLogin()){
            return $this->noLoginReturn();
        }
        
        if($this->role != 2){
            return errorResult('只有代理才可操作');
        }
        
        
        $params = request()->post();
        
        $storeId = isset($params['store_id']) ? intval($params['store_id']) : 0;
        
        $data = [];
        
        $data['store_account'] = isset($params['store_account']) ? trim($params['store_account']) : '';
        if($data['store_account'] === ''){
            return errorResult('请设置代理商登录账号');
        }
        
        $data['store_name'] = isset($params['store_name']) ? trim($params['store_name']) : '';
        if($data['store_name'] === ''){
            return errorResult('请填写代理商名称');
        }
        
        
        $data['contact'] = isset($params['contact']) ? trim($params['contact']) : '';
        $data['mobile'] = isset($params['mobile']) ? trim($params['mobile']) : '';
        $data['service_telephone'] = isset($params['service_telephone']) ? trim($params['service_telephone']) : '';
        
        $data['profit_rate'] = isset($params['profit_rate']) ? floatval($params['profit_rate']) : 0;
        if($data['profit_rate'] < 0 || $data['profit_rate'] > 100){
            return errorResult('提现费率只能在0-100之间');
        }
        
        $data['province_id'] = isset($params['province_id']) ? intval($params['province_id']) : 0; //
        $data['city_id'] = isset($params['city_id']) ? intval($params['city_id']) : 0; //
        $data['district_id'] = isset($params['district_id']) ? intval($params['district_id']) : 0; //
        $data['address'] = isset($params['address']) ? trim($params['address']) : ''; //
        
        $data['status'] = isset($params['status']) ? intval($params['status']) : 1; //
        
        if($data['province_id'] > 0){
            $areaModel = model('Area');
            $data['province'] = $areaModel->where(['id' => $data['province_id']])->value('name');
            if($data['city_id'] > 0){
                $data['city'] = $areaModel->where(['id' => $data['city_id']])->value('name');
                if($data['district_id'] > 0){
                    $data['district'] = $areaModel->where(['id' => $data['district_id']])->value('name');
                }
            }
        }
        
        
        $storeModel = model('Store');
        
        if($storeId > 0){
            $field = array_keys($data);
            $store = $storeModel->findOne(['store_id' => $storeId], $field);
            if(empty($store)) {
                return errorResult();
            }
            
            $updateData = [];
            
            foreach ($data as $k => $val) {
                if($store[$k] != $val){
                    $updateData[$k] = $val;
                }
            }
            
            if(empty($updateData)) {
                return successResult('编辑成功1');
            }
            
            if(isset($updateData['store_account'])){
                $rs = $storeModel->findOne(['store_account' => $data['store_account']], ['store_id']);
                if(!empty($rs)) {
                    return errorResult('已存在相同的账号');
                }
            }
            
            $rs = $storeModel->modify(['store_id' => $storeId], $updateData);
            if(!$rs) {
                return errorResult('编辑代理商失败');
            }
            
            return successResult('编辑代理商成功');
            
        }else{
            $store = $storeModel->findOne(['store_account' => $data['store_account']], ['store_id']);
            if(!empty($store)) {
                return errorResult('已存在相同的账号');
            }
            
            $salt = uniqid();
            $salt = substr($salt, strlen($salt) -7 , 6);
            $password = md5(md5(md5('123456')) . $salt);
            
            $data['salt'] = $salt;
            $data['password'] = $password;
            $data['create_time'] = time();
            $data['agent_id'] = $this->userid;
            
            $storeId = $storeModel->add($data);
            if(!$storeId){
                return errorResult('新增代理商失败');
            }
            
            return successResult('新增代理商成功');
        }
        
    }
    
    //获取门店编辑信息
    public function getEditInfo() {
        if($this->noLogin()){
            return $this->noLoginReturn();
        }
        
        if($this->role != 2) {
            return errorResult();
        }
        $storeId = request()->get('id', 0, 'intval');
        
        $field = ['store_account', 'store_name', 'contact', 'mobile', 'service_telephone', 'profit_rate', 'province_id', 'city_id', 'district_id', 'address', 'status'];
        
        $store = model('Store')->findOne(['store_id' => $storeId], $field);
        
        if(empty($store)) {
            return errorResult('门店不存在');
        }
        
        return successResult($store);
        
    }
    
    
    //设置密码
    public function setpwd() {
        if($this->noLogin()){
            return $this->noLoginReturn();
        }

        if($this->role != 2){
            return errorResult('只有代理商才可以设置密码');
        }
        
        $params = request()->post();
        
        $password = isset($params['password']) ? trim($params['password']) : '';
        if(32 != strlen($password)){
            return errorResult('请输入正确的密码');
        }
        $password = strtolower($password);
        
        $storeId = isset($params['storeId']) ? intval($params['storeId']) : 0;
        if($storeId <= 0) {
            return errorResult();
        }
        
        $salt = uniqid();
        $salt = substr($salt, strlen($salt) -7 , 6);
        $password = md5(md5($password) . $salt);
        
        $rs = model('Store')->modify(['store_id' => $storeId], ['salt' => $salt, 'password' => $password]);
        if($rs) {
            return successResult('设置成功');
        }else{
            return errorResult('设置失败');
        }
    }
    
    public function dropSelect() {
        $list = model('Store')->findAll(['agent_id' => $this->userid], ['store_id', 'store_name']);
        return successResult($list);
    }
    
}


















